package com.qf.xxx.config;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.qf.xxx.job.MyJobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticJobConfig {


    //1. 连接Zookeeper
    @Bean
    public CoordinatorRegistryCenter registryCenter(){
        CoordinatorRegistryCenter registryCenter = new ZookeeperRegistryCenter(
                new ZookeeperConfiguration(
                        "192.168.199.109:2181,192.168.199.109:2182,192.168.199.109:2183",
                        "elastic-job"));

        registryCenter.init();

        return registryCenter;
    }



    //2. 配置任务逻辑和执行周期,并开启任务
    @Bean
    public SpringJobScheduler jobScheduler(MyJobDetail myJobDetail, CoordinatorRegistryCenter registryCenter){
        // 定义作业核心配置
        JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration
                .newBuilder("JobDetail", "0/3 * * * * ?", 3)
                .shardingItemParameters("0=A,1=A,2=A")
                .build();

        // 定义SIMPLE类型配置
        SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, MyJobDetail.class.getCanonicalName());

        // 定义Lite作业根配置
        LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).build();

        // 定义SpringJobScheduler开始执行任务
        SpringJobScheduler jobScheduler = new SpringJobScheduler(myJobDetail,registryCenter,simpleJobRootConfig);

        // 开始执行任务
        jobScheduler.init();

        // 返回
        return jobScheduler;
    }



}
